﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ras2ClassLib
{
    public class HitListController
    {
        /// <summary>
        /// Sækja öll lög á vinsældalista sem beðið er um.
        /// </summary>
        /// <param name="nHitList"></param>
        /// <returns></returns>
        public IEnumerable<UIHitListEntry> GetSongsOnHitList(int nHitList)
        {
            Ras2DBDataContext db = new Ras2DBDataContext();

            var result = from hitListEntry in db.HitListEntries
                         where hitListEntry.HitListID == nHitList
                         orderby hitListEntry.Position ascending
                         select new UIHitListEntry
                         {
                             Position = hitListEntry.Position,
                             SongID = hitListEntry.Song.ID,
                             SongName = hitListEntry.Song.Name,
                             PerformerID = hitListEntry.Song.PerformerID,
                             PerformerName = hitListEntry.Song.Performer.Name
                         };

            return result;
        }

        /// <summary>
        /// Sækja alla vinsældalista.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<HitList> GetAllHitLists()
        {
            Ras2DBDataContext db = new Ras2DBDataContext();

            var result = from hitlist in db.HitLists
                         // ABB orderby hitlist.Name ascending
                         select hitlist;

            return result;
        }

        /// <summary>
        /// Skilar öllum vinsældalistum sem inniheldur lagið sem sent er inn.
        /// </summary>
        /// <param name="nID"></param>
        /// <returns></returns>
        public IEnumerable<UIHitListEntry> GetAllHitListsWithSong(int nID)
        {
            Ras2DBDataContext db = new Ras2DBDataContext();

            var result = from hitListEntry in db.HitListEntries
                         where hitListEntry.SongID == nID
                         orderby hitListEntry.ID ascending
                         select new UIHitListEntry
                         {
                             Position = hitListEntry.Position,
                             HitListName = hitListEntry.HitList.Name,
                             HitListID = hitListEntry.HitList.ID
                         };

            return result;
        }

        /// <summary>
        /// Skilar vinsældalistanum með ID sem sent er inn.
        /// </summary>
        /// <param name="nID"></param>
        /// <returns></returns>
        public HitList GetHitListByID(int nID)
        {
            Ras2DBDataContext context = new Ras2DBDataContext();
            return (from a in context.HitLists
                    where a.ID == nID
                    select a).SingleOrDefault();
        }

        public void UpdateHitList(HitList s)
        {
            Ras2DBDataContext context = new Ras2DBDataContext();
            HitList original = (from a in context.HitLists
                                where a.ID == s.ID
                                select a).SingleOrDefault();
            if (original != null)
            {
                original.Name = s.Name;
                original.DateBegins = s.DateBegins;
                original.DateEnds = s.DateEnds;
                original.Weeklist = s.Weeklist;
                context.SubmitChanges();
            }
        }

        public void AddHitListEntry(HitListEntry s)
        {
            Ras2DBDataContext context = new Ras2DBDataContext();
            context.HitListEntries.InsertOnSubmit(s);
            context.SubmitChanges();
        }

        public void AddHitList(HitList s)
        {
            Ras2DBDataContext context = new Ras2DBDataContext();
            context.HitLists.InsertOnSubmit(s);
            context.SubmitChanges();
        }

        public void DeleteHitList(HitList s)
        {
            Ras2DBDataContext context = new Ras2DBDataContext();
            context.HitLists.Attach(s);
            context.HitLists.DeleteOnSubmit(s);
            context.SubmitChanges();
        }		
    }
}
